<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelUserselement extends JModel
{
	/**
	 * User selement data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * User selement total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	/**
	 * userselement id
	 *
	 * @var int
	 */
	var $_id = null;

	/**
	 * Constructor
	 *
	 * @since 1.0
	 */
	function __construct() 
	{
		parent::__construct();

		global $mainframe, $option;

		$limit		= $mainframe->getUserStateFromRequest( $option.'.userselement.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0);

		// In case limit has been changed, adjust limitstart accordingly
		$limitstart = ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	/**
	 * Method to set the Courseselement identifier
	 *
	 * @access	public
	 * @param	int Courseselement identifier
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id	 = $id;
		$this->_data = null;
	}
	
	
	/**
	 * Method to get Userselement data
	 *
	 * @access public
	 * @return array
	 */
	function getData()
	{
		// Lets load the Userselement if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			//$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
			//echo $query;
			if ((int)$this->getState('limitstart') < (int)$this->_total) {
				$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit') );
			} else {
				$this->_data = $this->_getList($query, 0, $this->getState('limit') );
			}
		}

		return $this->_data;
	}

	/**
	 * Method to get the total nr of the Userselement
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal()
	{
		// Lets load the Userselement if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the Userselement
	 *
	 * @access public
	 * @return integer
	 */
	function getPagination()
	{
		// Lets load the Userselement if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new FMCPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}

	/**
	 * Method to build the query for the Courseselement
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	 */
	function _buildQuery()
	{
		// Get the WHERE, HAVING and ORDER BY userselement for the query
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();

		$query = ' SELECT p.*, u.username '
					.' FROM #__users AS u '
					.' LEFT JOIN #__ownservice_users AS p ON p.user_id=u.id '
					. $where
					. ' GROUP BY u.id '
					. $orderby
					;
					
		return $query;
	}

	/**
	 * Method to build the orderby clause of the query for the Userselement
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentOrderBy()
	{
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest( $option.'.userselement.filter_order', 'filter_order', 'u.username', 'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.userselement.filter_order_Dir',	'filter_order_Dir',	'ASC', 'word' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;

		return $orderby;
	}

	/**
	 * Method to build the where clause of the query for the Userselement
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentWhere()
	{
		global $mainframe, $option;

		//initialise variables
		$user	= & JFactory::getUser();
		$owuser = & Ownservice::getUser();
		
		$filter_branch		= $mainframe->getUserStateFromRequest( $option.'.userselement.filter_branch', 'filter_branch', 0, 'int' );
		$search 			= $mainframe->getUserStateFromRequest( $option.'.userselement.search', 'search', '', 'string' );
		$search 			= $this->_db->getEscaped( trim(JString::strtolower( $search ) ) );

		$where = array();

		$where[] = 'p.deleted = 0';
		$where[] = 'p.group_id=1';
		//check access 
		if($user->gid < 25) {
			if( $owuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 'p.branch_id ='.$owuser->get('branch_id');
			}
		} else {
			if($filter_branch)
			{
				$where[] = 'p.branch_id='.$filter_branch;
			}
		}
		
		if ($search) {
			$where[] = ' ( 
							LOWER(u.username) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false )
						.' OR
							LOWER(p.firstname) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false )
						.' OR 
							LOWER(p.lastname) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false )
						.')';
		}

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		return $where;
	}
	
	/**
	 * Method to get branches
	 * 
	 * @access public
	 * @return object array
	 * @since 1.5
	 */
	 function getBranches()
	 {
	 	$query = 'SELECT * FROM #__ownservice_branches  '
				.' WHERE deleted=0 AND published = 1'
				.' ORDER BY name';

		$this->_db->setQuery($query);
		$branches = $this->_db->loadObjectList();

		return $branches;
	 }
}
